package com.yingeo.ai.sdk.demo.utils;

import android.graphics.Rect;
import android.view.View;

import androidx.recyclerview.widget.RecyclerView;

public class GridLayoutDecoration extends RecyclerView.ItemDecoration {

    int mHorizontalSpace;
    int mVerticalSpace;
    int spanCount;

    public GridLayoutDecoration(int spanCount, int horizontalSpace, int verticalSpace) {
        this.spanCount = spanCount;
        this.mHorizontalSpace = horizontalSpace;
        this.mVerticalSpace = verticalSpace;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        int position = parent.getChildAdapterPosition(view);
        float density = view.getResources().getDisplayMetrics().density;
        int hSpace = (int) (mHorizontalSpace * density);
        int vSpace = (int) (mVerticalSpace * density);
        outRect.left = hSpace;
        outRect.right = hSpace;
        if (position < spanCount) {
            outRect.top = vSpace;
        }
        outRect.bottom = vSpace;

    }

}
